#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import json
from annotation import Annotation


class JavaCodeHandler:
    # 文件名称
    name = ''

    # 文件路径
    path = ''

    # 文件总行数
    lineNum = 0

    # 处理后的文件
    resPath = ''

    # 保存注释信息的map
    map = {}

    # 读文件时当前注释首行号码
    currentStartIndex = 0

    # 写文件时,当前注释对象
    currentAnnotation = Annotation(0, 0)

    # 是否写行号
    isWriteLineNum = False

    # 是否保留单行注释
    isKeepSingleLineAnnotation = True

    def __int__(self):
        self.name = ''
        self.path = ''
        self.lineNum = 0

    def __init__(self, path, isWriteLineNum=False, isKeepSingleLineAnnotation=True):
        self.name = ''
        self.lineNum = 0
        self.path = path
        self.isWriteLineNum = isWriteLineNum
        self.isKeepSingleLineAnnotation = isKeepSingleLineAnnotation

    def __init__(self, name, path, isWriteLineNum=False, isKeepSingleLineAnnotation=True):
        self.lineNum = 0
        self.name = name
        self.path = path
        self.isWriteLineNum = isWriteLineNum
        self.isKeepSingleLineAnnotation = isKeepSingleLineAnnotation

    def handler(self, path):
        # 生产注释信息map
        if os.path.exists(path):
            # 处理后的文件名称
            self.resPath = self.getNewFileName(self.path)
            with open(path, 'r', encoding='utf-8') as f:
                i = 1
                for line in f:
                    self.put(line, i)
                    i = i + 1
                self.lineNum = i
        # 写文件
        self.createNewFile()

    def put(self, line='', index=0):
        if self.isAnnotationStart(line):
            ann = Annotation(index, index)
            self.map.setdefault(str(index), ann)
            self.currentStartIndex = index

        if self.isAnnotationEnd(line):
            ann = self.map.get(str(self.currentStartIndex))
            ann.endNum = index

            # print(self.currentStartIndex, index, line, sep=',')

    def getNewFileName(self, fileName=''):
        index = fileName.find(".")
        name = fileName[0:index] + "_bk.java"
        return name

    def createNewFile(self):
        with open(self.resPath, 'w', encoding='utf-8') as f1:
            with open(self.path, 'r', encoding='utf-8') as f2:
                lineNum = 1
                for line in f2:
                    ann = self.map.get(str(lineNum))
                    if ann != None:
                        # 注释首行
                        self.currentAnnotation = ann
                        self.writeSingleAnnotation(f1, line, lineNum)
                        pass
                    else:
                        # 注释中间行或者尾行
                        if lineNum >= self.currentAnnotation.startNum and lineNum <= self.currentAnnotation.endNum:
                            # 注释中间行
                            pass
                        else:
                            # 非注释行
                            self.doWrite(f1, line, lineNum)
                    lineNum = lineNum + 1

    '''
    写单行注释
    '''
    def writeSingleAnnotation(self, f1, line, lineNum):
        if self.isKeepSingleLineAnnotation and (
                self.currentAnnotation.startNum == self.currentAnnotation.endNum):
            self.doWrite(f1, line, lineNum)

    '''
    写操作
    '''
    def doWrite(self, f, line, lineNum):
        lineStr = ""
        if self.isWriteLineNum:
            lineStr = str(lineNum) + "  " + line
        else:
            lineStr = line
        f.write(lineStr)
    '''
    判断当前行是否包含注释的开头
    '''
    def isAnnotationStart(self, str=''):

        res = (str.find("/*") >= 0) or (str.find("/**") >= 0)
        return res

    '''
    判断当前行是否包含注释的结尾
    '''

    def isAnnotationEnd(self, str=''):
        res = (str.find("*/") >= 0) or (str.find("**/") >= 0)
        return res

    def toString(self):
        return json.dumps(self)


def main():
    # 代码文件
    path = 'D:\\workspace\\python-workspace\\python-leaning\\test\\javacode\\String.java'

    # 新建处理器
    handler = JavaCodeHandler("String", path)

    # 处理文件
    handler.handler(path)



if (__name__ == '__main__'):
    main()
